//
//  YXJListPushViewController.m
//  Piece
//
//  Created by dllo on 16/6/22.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "YXJListPushViewController.h"
#import "ZHNetworkHandler.h"
#import "YXJLikeMusicModel.h"
#import "YXJMusicCell.h"
#import "DXYMusicViewController.h"
#import "YXJPushMusicViewController.h"

@interface YXJListPushViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation YXJListPushViewController

-(void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self creatDataByPageNum:self.pageNumber];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)creatTableView
{
    //初始化tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = 0;
    [self.tableView setContentOffset:CGPointMake(0, 180)];
    [self.view addSubview:_tableView];
    
    //注册
    [self.tableView registerClass:[YXJMusicCell class] forCellReuseIdentifier:@"cell"];
}

#pragma mark -- 网络解析(获取歌曲数据)
- (void)creatDataByPageNum:(NSInteger)pageNum
{
    NSString *urlString = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/search/music/%ld", pageNum];

        self.dataSource = [NSMutableArray array];
    
        [ZHNetworkHandler getDataByURLString:urlString WithBodyString:nil WithHeaderDic:nil WithResponse:ZHJSON WithSuccess:^(id result) {
            
            NSArray *array = [result objectForKey:@"data"];
            
            for (NSDictionary *diction in array) {
                
                YXJLikeMusicModel *model = [[YXJLikeMusicModel alloc] initWithDic:diction];

                [self.dataSource addObject:model];
            }

            [self.tableView reloadData];
            
        } WithFailure:^(NSError *error) {
            
        }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YXJMusicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.musicModel = [self.dataSource objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YXJPushMusicViewController *mainVC = [[YXJPushMusicViewController alloc] init];
    NSInteger pageStart = [[self.dataSource objectAtIndex:indexPath.row] id];
    mainVC.pageS = pageStart;

    [self.navigationController pushViewController:mainVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
